/**
 * 
 */
package com.res.common.controller;

import com.res.simulator.handler.CommonFileHandler;
import com.res.simulator.handler.StoreFrontHandler;
import com.res.simulator.model.StoreFrontEligibilityModel;
import com.res.simulator.view.StoreFrontView;
import com.res.system.properties.PropertySelector;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Dhanushka Ariyasingha.
 *
 */
@Controller
@SessionAttributes
public class StoreFrontController extends BaseController {

	// get log4j handler
	private static final Logger logger = Logger.getLogger(StoreFrontController.class);

	@RequestMapping("/storefront")
	public String home(
			@ModelAttribute("StoreFrontEligibility") StoreFrontEligibilityModel storeFrontInput) {

		final Map<String, String> storeFrontMap = new LinkedHashMap<String, String>();
		
		

		storeFrontMap.put("SHOP", "ATG");
		storeFrontMap.put("PLUM", "PLUM");
		storeFrontMap.put("CSSC", "CSSC");
		storeFrontMap.put("BusinessDelegate", "DELEGATE");
		storeFrontMap.put("MyVerizon2", "MyVerizon2");

		getSession().setAttribute("storeFrontMap", storeFrontMap);

		final String value = PropertySelector.getInstance().select("RetailCatalogAPI","createSeller");
		//logger.info("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" + value);
		
		return "storefrontlayout";
	}

	@RequestMapping(value = "/invokeStoreFront", method = RequestMethod.GET, params = "InvokeSF")
	protected String invokeStoreFront(
			@ModelAttribute("storefrontEligible") final StoreFrontEligibilityModel eligibility,
			final BindingResult result) throws Exception {

		final StoreFrontView oldView = (StoreFrontView) getSession()
				.getAttribute("StoreFrontView");

		StoreFrontHandler handler = new StoreFrontHandler();
		final StoreFrontView view = handler.process(eligibility);

		if (null != oldView) {
			view.update(oldView);
		}

		getSession().setAttribute("StoreFrontView", view);

		// logger.info(message);

		return "redirect:storefront.htm";
	}

	@RequestMapping(value = "/downloadXML", method = RequestMethod.GET)
	public String downloadFile(final HttpServletRequest request, HttpServletResponse response) throws IOException {

		final StoreFrontView view = (StoreFrontView) getSession().getAttribute(
				"StoreFrontView");
		if (null != view) {

			final Map<String, String> storeFrontMap = view.getLocationMap();
			final String selectedFile = (String) request.getParameter("selectedFile");

			System.out.println("Selected file :" + selectedFile);

			final String fileLocation = storeFrontMap.get(selectedFile);
	        response.setContentType("application/xml");
	        response.addHeader("Content-disposition", "atachment; filename="+selectedFile);

			final OutputStream buffoutput= new BufferedOutputStream(response.getOutputStream());
			final OutputStreamWriter outputwriter = new OutputStreamWriter(buffoutput);

			final CommonFileHandler fileHandler =new CommonFileHandler();
			final StringBuilder stringBuilder = fileHandler.read(fileLocation);

			outputwriter.write(stringBuilder.toString());

			outputwriter.flush();
			outputwriter.close();

		}

		return "redirect:storefront.htm";

	}

	@RequestMapping(value = "/invokeStoreFront", method = RequestMethod.GET, params = "CompareSF")
	protected String compareSF(@ModelAttribute("storefrontEligible") final StoreFrontEligibilityModel eligibility,
			final BindingResult result) {

		//TODO : Comparison functionality implementation.

		return "redirect:storefront.htm";
	}

	@RequestMapping(value = "/invokeStoreFront", method = RequestMethod.GET, params = "ClearSFHistory")
	protected String clearDataTableHistory() {

		final StoreFrontView oldView = (StoreFrontView) getSession()
				.getAttribute("StoreFrontView");

		if (null != oldView) {
			oldView.clear();
		}

		getSession().setAttribute("StoreFrontView", oldView);

		return "redirect:storefront.htm";
	}

}
